package com.bier.fishproject.http;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import android.content.Context;
import android.os.Handler;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
/**
 * http网络工具类
 * @author Administrator
 *
 */
public class HttpUtils {

	public static RequestQueue mRequestQueue;
	
	private static HttpUtils httpUtils;
	
	/**
	 * 单列模式
	 * @param context
	 * @return
	 */
	public static HttpUtils getInstance(Context context){
		if(httpUtils==null){
			httpUtils=new HttpUtils();
			mRequestQueue =  Volley.newRequestQueue(context);
		}
		return httpUtils;
	}
	
	
	/**
	 * get请求方法
	 */
	public void getMethod(final String url,final Handler handler){
		JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() {  
            public void onResponse(JSONObject jb) { 
            	
            }  
        },new Response.ErrorListener() {  
            public void onErrorResponse(VolleyError error) {  
            	
            }  
        }){
			//设置header信息
			public Map<String, String> getHeaders() throws AuthFailureError {
	                HashMap<String, String> headers = new HashMap<String, String>();
	                return headers;
	        }
		};
		//设置请求超时
		jr.setRetryPolicy(new DefaultRetryPolicy(10 * 1000, 1, 1.0f));
		//添加到消息队列中
        mRequestQueue.add(jr);
	}
	
	
	/**
	 * post请求方法
	 */
	public void postMethod(final String url,final Handler handler,Map<Object, Object> params){
		JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(params), new Response.Listener<JSONObject>() {
			        public void onResponse(JSONObject jb) {
			        	
			        }
			    }, new Response.ErrorListener() {
			        public void onErrorResponse(VolleyError error) {
			 
			        }
			    }){
			    //设置header信息
			     public Map<String, String> getHeaders()throws AuthFailureError {
			    	    HashMap<String, String> headers = new HashMap<String, String>();
				        return headers;
			     }
		};
		//设置请求超时
		jr.setRetryPolicy(new DefaultRetryPolicy(10 * 1000, 1, 1.0f));
		//添加到消息队列中
        mRequestQueue.add(jr);
	}
	
}
